{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Plot the support vectors in LinearSVC\n\n\nUnlike SVC (based on LIBSVM), LinearSVC (based on LIBLINEAR) does not provide\nthe support vectors. This example demonstrates how to obtain the support\nvectors in LinearSVC.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.datasets import make_blobs\nfrom sklearn.svm import LinearSVC\n\nX, y = make_blobs(n_samples=40, centers=2, random_state=0)\n\nplt.figure(figsize=(10, 5))\nfor i, C in enumerate([1, 100]):\n    # \"hinge\" is the standard SVM loss\n    clf = LinearSVC(C=C, loss=\"hinge\", random_state=42).fit(X, y)\n    # obtain the support vectors through the decision function\n    decision_function = clf.decision_function(X)\n    # we can also calculate the decision function manually\n    # decision_function = np.dot(X, clf.coef_[0]) + clf.intercept_[0]\n    support_vector_indices = np.where((2 * y - 1) * decision_function <= 1)[0]\n    support_vectors = X[support_vector_indices]\n\n    plt.subplot(1, 2, i + 1)\n    plt.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap=plt.cm.Paired)\n    ax = plt.gca()\n    xlim = ax.get_xlim()\n    ylim = ax.get_ylim()\n    xx, yy = np.meshgrid(np.linspace(xlim[0], xlim[1], 50),\n                         np.linspace(ylim[0], ylim[1], 50))\n    Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])\n    Z = Z.reshape(xx.shape)\n    plt.contour(xx, yy, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,\n                linestyles=['--', '-', '--'])\n    plt.scatter(support_vectors[:, 0], support_vectors[:, 1], s=100,\n                linewidth=1, facecolors='none', edgecolors='k')\n    plt.title(\"C=\" + str(C))\nplt.tight_layout()\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}